package com.lyfam.game.behaviosr.steering;

import com.lyfam.game.entity.MovingEntity;
import com.lyfam.game.geometry.Vector2D;

public class FleeingBehavior implements ISteeringBehavior
{

    public int getPriority() {
		return 0;
	}

    public int compareTo(ISteeringBehavior o)
	{
		if (getPriority() == o.getPriority())
		{
			return 0;
		}
		else
		{
			if (getPriority() >o.getPriority())
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}
	}
    
	private MovingEntity vehicle;
    private Vector2D target;
    
    public FleeingBehavior(MovingEntity move, Vector2D target)
    {
        this.vehicle = move;
        this.target = target;
    }
    
    private Vector2D calculateSteeringForce()
    {
        float panicDistanceSquare = PANIC_DISTANCE * PANIC_DISTANCE;
        
        if (vehicle.getPosition().distanceSquare(target) > panicDistanceSquare)
        {
            return new Vector2D(0, 0);
        }
        
        Vector2D force = Vector2D.clone(vehicle.getPosition());
        force.sub(target);
        force.normalize();
        force.mult(vehicle.getMaxSpeed());
        force.sub(vehicle.getVelocity());
        return force;
    }
    
    public Vector2D getSteeringForce()
    {
        return this.calculateSteeringForce();
    }

}
